#include "bord.h"

Bord::Bord(QColor bcol, QWidget *parent) :
    QLabel(parent)
{

    this->bcol = bcol;
    lcol = QColor(0,0,0,255); //208,208,128,255);

//    grad = new QLinearGradient;
//    grad->setColorAt(0., QColor(0,0,0,0));
//    grad->setColorAt(.2, QColor(0,0,0,255));
//    grad->setColorAt(.5, lcol);
//    grad->setColorAt(.8, QColor(0,0,0,255));
//    grad->setColorAt(1., QColor(0,0,0,0));
//    grad->setColorAt(0., lcol);
//    grad->setColorAt(.05, lcol);
//    grad->setColorAt(.5, lcol);
//    grad->setColorAt(.95, lcol);
//    grad->setColorAt(1., QColor(0,0,0,255));

//    grad->setSpread(QGradient::PadSpread);
//    grad->setCoordinateMode(QGradient::ObjectBoundingMode);


    as.reserve(8);
    as << QPoint(1,0) << QPoint(1,1) << QPoint(0,1) << QPoint(-1,1) << QPoint(-1,0) << QPoint(-1,-1) << QPoint(0,-1) << QPoint(1,-1);


    nests.reserve(nn);
    for (int r=0; r<3; ++r) {
        for (int a=0; a<8; ++a) {
            nests << new Nest(r,a, 8*r + a, bcol, lcol, this);
        }
    }


    nests[ 0]->neighs << nests[ 1] << nests[ 7] << nests[ 8];
    nests[ 1]->neighs << nests[ 0] << nests[ 2];
    nests[ 2]->neighs << nests[ 1] << nests[ 3] << nests[10];
    nests[ 3]->neighs << nests[ 2] << nests[ 4];
    nests[ 4]->neighs << nests[ 3] << nests[ 5] << nests[12];
    nests[ 5]->neighs << nests[ 4] << nests[ 6];
    nests[ 6]->neighs << nests[ 5] << nests[ 7] << nests[14];
    nests[ 7]->neighs << nests[ 6] << nests[ 0];
    nests[ 8]->neighs << nests[ 0] << nests[ 9] << nests[15] << nests[16];
    nests[ 9]->neighs << nests[ 8] << nests[10];
    nests[10]->neighs << nests[ 2] << nests[ 9] << nests[11] << nests[18];
    nests[11]->neighs << nests[10] << nests[12];
    nests[12]->neighs << nests[ 4] << nests[11] << nests[13] << nests[20];
    nests[13]->neighs << nests[12] << nests[14];
    nests[14]->neighs << nests[ 6] << nests[13] << nests[15] << nests[22];
    nests[15]->neighs << nests[14] << nests[ 8];
    nests[16]->neighs << nests[17] << nests[23] << nests[ 8];
    nests[17]->neighs << nests[16] << nests[18];
    nests[18]->neighs << nests[17] << nests[19] << nests[10];
    nests[19]->neighs << nests[18] << nests[20];
    nests[20]->neighs << nests[19] << nests[21] << nests[12];
    nests[21]->neighs << nests[20] << nests[22];
    nests[22]->neighs << nests[21] << nests[23] << nests[14];
    nests[23]->neighs << nests[22] << nests[16];

//    qDebug() << (0 - 8) % 24 << (16+8)% 24;


    //resize(320, 320);


//    resize(320,458);

}

void Bord::mouseMoveEvent(QMouseEvent *ev) {
    ev->accept();
//    qDebug() << ev->pos();
}


void Bord::resizeEvent(QResizeEvent *e) {
    QSize sz = e->size();

    if (sz.isEmpty()) return;



//    qDebug() << "bord resize" << e->size();

//    int m = qMin(sz.width(),sz.height());
//    int nsz = 0.05*m;
//    int nsz2 = 0.05*m/2.;
//    int f = (1.-0.05)*m/(2.*3.);
//    int m2 = m/2;

    int m = qMin(sz.width(),sz.height());

    int nsz = 0.085*m;
    Nest::sz = QSize(nsz,nsz);
    int nsz2 = nsz/2;
    QSize f = (sz - Nest::sz)/(2.*3.);
    QSize sz2 = sz/2;


//    qDebug() << "sz,nsz,nsz2,f,m2" << sz << nsz << nsz2 << f << sz2;

//    QPoint ps;
    int nx, ny;

    for (int i=0; i < nn; ++i) {
        nx =  f.width()  * (nests[i]->r+1) * as[nests[i]->a].x() + sz2.width()  - nsz2;
        ny = -f.height() * (nests[i]->r+1) * as[nests[i]->a].y() + sz2.height() - nsz2;
        nests[i]->setGeometry(nx, ny, nsz, nsz);
    }

    QPixmap pxm(sz);
    pxm.fill(QColor(0,0,0,0));

    QPainter pnt(&pxm);
    pnt.setPen(Qt::NoPen);
    pnt.setBrush(QBrush(bcol));
    pnt.drawRoundedRect(pxm.rect(),5,5,Qt::RelativeSize);

    pnt.setPen(QPen(QBrush(lcol),3,Qt::SolidLine,Qt::FlatCap,Qt::RoundJoin));

    QLine ln;
    for (int i=0; i < nn; ++i) {
        for (int j=0; j < nests[i]->neighs.size(); ++j) {
            ln = QLine(nests[i]->geometry().center(), nests[i]->neighs[j]->geometry().center());

//            qreal len = qSqrt(ln.dx()*ln.dx() + ln.dy()*ln.dy());
//            grad->setStart(-ln.y1()/len + ln.x1(), ln.dx()/len + ln.y1());
//            grad->setFinalStop(ln.y1()/len + ln.x1(), -ln.dx()/len + ln.y1());


//            QPoint pt;

//            if (ln.dx() == 0) {
//                pt = QPoint(1,0);
//            } else if (ln.dy() == 0) {
//                pt = QPoint(0,1);
//            } else {
//                qDebug() << "\n\n\n\n\nnproblem\n\n\n\n\n\n" ;
//            }
//            grad->setStart(ln.p1() - pt*2);
//            grad->setFinalStop(ln.p1() + pt*2);

////            qDebug() << ln << len << QPoint(-ln.y1()/len + ln.x1(), ln.dx()/len + ln.y1()) << QPoint (ln.y1()/len + ln.x1(), -ln.dx()/len + ln.y1());
//            pnt.setPen(QPen(QBrush(*grad),2,Qt::SolidLine,Qt::FlatCap,Qt::RoundJoin));
            pnt.drawLine(ln);

        }
    }


    pnt.end();

//    pxm.save("ikona.png");
    setPixmap(pxm);

//    qDebug() << " nest sz" << Nest::sz;

}







bool Bord::have_line(int nid, int pid) {
    int lnid[2], unid[2];

    if (nid % 2 == 0) {

        lnid[0] = (nid + 16)%24;
        unid[0] = (nid +  8)%24;

        lnid[1] = (nid/8)*8 + (nid + 7)%8;
        unid[1] = (nid/8)*8 + (nid + 1)%8;

    } else {

        lnid[0] = (nid/8)*8 + (nid + 6)%8;
        unid[0] = (nid/8)*8 + (nid + 7)%8;

        lnid[1] = (nid/8)*8 + (nid + 1)%8;
        unid[1] = (nid/8)*8 + (nid + 2)%8;

    }

    bool r = false;

    for (int i=0; i<2; ++i) {
//        qDebug() << (nid%2 == 0) << lnid[i] << nid << unid[i] ;
        if (!(nests[lnid[i]]->empty            || nests[unid[i]]->empty)) {
            if (nests[lnid[i]]->bob->plid == pid && nests[unid[i]]->bob->plid == pid) {
                r = true;
                break;
            }
        }
    }

    return r;

}








//void Bord::paintEvent(QPaintEvent *e) {
//    qDebug() << e->rect() << size();
//    QPainter p(this);
//    p.drawRect(e->rect());
//    p.end();
//}
